<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货人姓名" prop="returnName">
        <el-input
          v-model="queryParams.returnName"
          placeholder="请输入退货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货人电话" prop="returnPhone">
        <el-input
          v-model="queryParams.returnPhone"
          placeholder="请输入退货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择退货状态" clearable>
          <el-option
            v-for="dict in dict.type.order_return_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理人" prop="handleMan">
        <el-input
          v-model="queryParams.handleMan"
          placeholder="请输入处理人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理时间">
        <el-date-picker
          v-model="daterangeHandleTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:orderReturn:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:orderReturn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderReturnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="退货理由" align="center" prop="returnReason" />
      <el-table-column label="退货金额" align="center" prop="returnAmount" />
      <el-table-column label="退货人姓名" align="center" prop="returnName" />
      <el-table-column label="退货人电话" align="center" prop="returnPhone" />
      <el-table-column label="退货状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_return_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="handleMan" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拒绝理由" align="center" prop="rejectReason" />
      <el-table-column label="处理时间" align="center" prop="handleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.status==1"
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleReturn(scope.row)"
            v-hasPermi="['mall:orderReturn:edit']"
          >确认退款</el-button>
          <br>
          <el-button
            v-show="scope.row.status==1"
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleRefuse(scope.row)"
            v-hasPermi="['mall:orderReturn:edit']"
          >拒绝退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拒绝理由" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入拒绝理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderReturn,refuseRefund,sureRefund } from "@/api/mall/orderReturn";

export default {
  name: "OrderReturn",
  dicts: ['order_return_status', 'delete_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退货表格数据
      orderReturnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 处理时间时间范围
      daterangeCreateTime: [],
      // 处理时间时间范围
      daterangeHandleTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        returnName: null,
        returnPhone: null,
        status: null,
        handleMan: null,
        handleTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rejectReason: [
          { required: true, message: "拒绝理由不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退货列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeHandleTime && '' != this.daterangeHandleTime) {
        this.queryParams.params["beginHandleTime"] = this.daterangeHandleTime[0];
        this.queryParams.params["endHandleTime"] = this.daterangeHandleTime[1];
      }
      listOrderReturn(this.queryParams).then(response => {
        this.orderReturnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        returnReason: null,
        returnAmount: null,
        returnName: null,
        returnPhone: null,
        status: null,
        handleMan: null,
        trackingNumber: null,
        receiverName: null,
        receiverPhone: null,
        receiverProvince: null,
        receiverCity: null,
        receiverRegion: null,
        receiverAddress: null,
        receiverTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        rejectReason: null,
        handleTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeHandleTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          refuseRefund(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    handleReturn(row){
      const ids = row.id
      this.$modal.confirm('是否确认该订单退款？').then(function() {
        return sureRefund(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退款成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleRefuse(row) {
      this.reset();
      this.form.id = row.id
      this.form.orderId = row.orderId
      this.open = true;
      this.title = "拒绝退款";
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mall/orderReturn/export', {
        ...this.queryParams
      }, `orderReturn_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
